using System.Drawing;

namespace ImageSaver
{
    public class Loader
    {
        private static volatile Loader _instance;
        private static object _syncRoot = new object();

        private Loader() 
        {
        }

        public static Loader GetInstance()
        {
            if (_instance == null)
            {
                lock (_syncRoot)
                {
                    if (_instance == null)
                        _instance = new Loader();
                }
            }
            return _instance;
        }

        private Bitmap Load(string filepath, string extension)
        {
            switch (extension)
            {
                case "bmp":
                case "jpg":
                case "png":
                    {
                        return new Bitmap(Image.FromFile(filepath));
                    }
                case "psd":
                    {
                        var auBitmap = new Aurigma.GraphicsMill.Bitmap(filepath);
                        return auBitmap.ToGdiplusBitmap();
                    }
                case "djvu":
                case "pdf":
                    {
                        return new Bitmap(LoadAdditionalFormats(filepath));
                    }
            }
            return null;
        }

        private Image LoadAdditionalFormats(string filepath)
        {
            var djvu = new DjVu.Document(filepath);
            return djvu.Pages[0] != null ? djvu.Pages[0].RenderImage() : null;
        }
    }
}